package org.ufba.buscador.util;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.ufba.buscador.crawling.preprocessor.simple.StopWordsRemover;


/**
*
* @author Efraim Machado and Arivan Bastos
*/

public class HTMLUtil {

	public static String getHTMLContent(URI uri)
	{
		if (uri != null)
		{
			try
			{
				return getHTMLContent(uri.toURL());
			}
			catch (MalformedURLException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		throw new RuntimeException("[ERRO:] URI nula");
	}
	
	public static String getHTMLContent(URL url)
	{
        InputStream in = null;
        try
        {
            in = new BufferedInputStream(url.openStream( ));
            InputStreamReader theHTML = new InputStreamReader(in);
            StringBuilder htmlContent = new StringBuilder();
            int c;
            while ((c = theHTML.read( )) != -1) 
            {
                htmlContent.append((char)c);
            }
            return htmlContent.toString();
        }
        catch (NullPointerException ex)
        {
             Logger.getLogger(StopWordsRemover.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (MalformedURLException ex) 
        {
             Logger.getLogger(StopWordsRemover.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IOException ex) {
            Logger.getLogger(StopWordsRemover.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally {
        	
            try
            {
                in.close();
            }
            catch (IOException ex) 
            {
                Logger.getLogger(StopWordsRemover.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
		return null;   
	}
}
